#!/bin/sh
# alive.test
#
# Copyright (C) 2010 Thien-Thi Nguyen
# Copyright (C) 2018-2020 Free Software Foundation, Inc.
#
# This program is free software, licensed under the terms of the GNU
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Thien-Thi Nguyen
# Modified: Reini Urban

# Commentary:

# This is a provisional test until something better comes along.
# It checks "aliveness" and the program returning success, and not
# correctness or performance.
#
# It relies on the TESTS_ENVIRONMENT Makefile.am variable to set
# valid `PROGS'.  Each prog in PROGS is run against a set of example DWGs,
# and if *not* successful the verbose output from -v2 is saved in a
# file whose name is DWG-PROG.log.
#
# If there are no problems, exit successfully.  Otherwise display
# the count of problem runs, the list of *.log files and exit with failure.
# DATADIR can be set to support testing from a separate build dir.

# Code:

PROGS=${PROGS:-"dwgread dwgbmp dwg2dxf dwg2SVG dwglayers dwggrep dwgrewrite dxf2dwg dwgwrite"}
[ -z "$DATADIR" ] && DATADIR="@top_srcdir@/test/test-data"
top_builddir="@top_builddir@"
# artefact for "shellcheck"
[ -z "$top_builddir" ] && echo $top_builddir
CFLAGS="@CFLAGS@"

i=0
rm -f ./*-dxf*.log ./*-read,.log ./*-write.log ./*-json.log ./*-rw.log
rm -f ./*-bmp.log ./*-grep.log ./*-layers.log ./*-SVG.log ./*-ps.log ./*-svg2.log

debugclasses=
case "$CFLAGS" in
    *-DDEBUG_CLASSES*) debugclasses=1 ;;
    *) ;;
esac

#just check that it does not crash
run ()
{
    prog="$1"
    dwg="$2"
    addarg="$3"

    bprog="$(basename "$prog" .exe)"
    bdwg="$(basename "$dwg" .dwg)"
    bprog1=$(echo "$bprog" | sed -e's,^dwg2,,')
    bprog1=$(echo "$bprog1" | sed -e's,^dwg,,')
    if [ -n "$addarg" ]; then
        out=$(echo "$addarg" | sed -e 's,--as=,,')
        log="$bdwg-$out-$bprog1.log"
    else
        log="$bdwg-$bprog1.log"
    fi
    arg="${DATADIR}/$dwg.dwg"
    rm "./$log" 2>/dev/null
    case $bprog in
    dwglayers)  ;;
    dwggrep)    arg="-i tekst $arg" ;;
    dwg2dxf)    if [ -n "$addarg" ]; then
                    arg="-y $addarg -o $bdwg-$out.dxf $arg"
                else
                    arg="-y $arg"
                fi
                ;;
    dwgread)    if [ -n "$addarg" ]; then
                    arg="-v2 $addarg -o $bdwg-$out.json $arg"
                else
                    arg="-v2 -o $dwg.json $arg"
                fi
                ;;
    dwgwrite)   if [ -n "$addarg" ]; then
                    arg="-y -v2 $addarg -o $bdwg-$out.dwg $dwg.json"
                else
                    arg="-y -v2 $dwg.json"
                fi
                ;;
    dxfwrite)   if [ -n "$addarg" ]; then
                    arg="-y -v2 $addarg -o $bdwg-$out.dxf $dwg.json"
                else
                    arg="-y -v2 $dwg.json"
                fi
                ;;
    dxf2dwg)    if [ -n "$addarg" ]; then
                    arg="-y -v2 $addarg -o $bdwg-$out.dwg ${DATADIR}/$dwg.dxf"
                else
                    arg="-y -v2 ${DATADIR}/$dwg.dxf"
                fi
                ;;
    dwgrewrite) if [ -n "$addarg" ]; then
                    arg="-v2 $addarg $arg"
                else
                    arg="-v2 $arg"
                fi
                ;;
    esac
    if [ -n "$VALGRIND" ] && [ -n "$LOG_COMPILER" ]; then
        TESTPROG="$LOG_COMPILER $LOG_FLAGS"
        log="$TEST_SUITE_LOG"
    else
        # libtool only if shared
        TESTPROG="@LTEXEC@"
    fi
    # indxf still leaks, GH #151
    if test "$bprog" = "dxf2dwg" -a -n "@HAVE_ASAN@"
    then
       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    fi
    # rewrite leaked 3DSOLID
    #if test $bprog = dwgrewrite -a -n "@HAVE_ASAN@" -a "$bdwg" = example_2000
    #then
    #   TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    #fi
    # injson leaks fixed, GH #197
    #if test $bprog = dwgwrite -a -n "@HAVE_ASAN@"
    #then
    #   TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    #fi
    echo "$TESTPROG ./$prog $arg 2>$log"
    # writers can hang
    #if test $bprog = dwgrewrite -o $bprog = dwgwrite -o $bprog = dxf2dwg
    #then
    #    if test x$MINGW != x; then
    #        OLDTESTPROG=$TESTPROG
    #        TESTPROG=echo
    #    else
    #        (sleep 10s; killall $bprog 2>/dev/null) &
    #    fi
    #fi
    # shellcheck disable=SC2086
    if $TESTPROG ./$prog $arg >"$log" 2>&1
    then
        rm "./$log"
    else
        todo=
        case $bprog in
            dwgwrite)
                [ -z $debugclasses ] || todo=1
                case "$(uname -s)" in
                    CYGWIN*) todo=1 ;;
                    *) ;;
                esac
                ;;
            dwgrewrite)
                case "$(uname -s)" in
                    MINGW*) test -f core && rm core
                            todo=1
                            ;;
                    *) ;;
                esac
                ;;
            *)  ;;
        esac

        if [ -z $todo ]
        then
            i=$((i+1))
            echo "$prog failure $?"
            tail -n100 "$log"
        else
            tmpnum=$((i+1))
            echo "$(basename "$0"): TODO $prog $tmpnum failures"
            ls -l "$log"
        fi
    fi
    if [ x"$MINGW" != x ] && [ x"$OLDTESTPROG" != x ]; then
        TESTPROG="$OLDTESTPROG"
    fi
}

DATA="sample_2000 example_2000 example_2004 example_2007 example_2010"
DATA="$DATA example_2013 example_2018 example_r14"
#TODO="r11/ACEL10"

for d in $DATA; do
    b="$(basename "$d")"
    rm "./$b.dxf" 2>/dev/null
    rm "./$b.svg" 2>/dev/null
    rm "./$b.ps" 2>/dev/null
    rm "./$b.bmp" 2>/dev/null
    rm "./$b-rewrite.dwg" 2>/dev/null
    rm "./$b-dwgwrite.dwg" 2>/dev/null
done

for prog in $PROGS ; do
    for dwg in $DATA; do
        run "$prog" "$dwg"
    done
done

# dwgread converts to json here, but has no --as yet
# still disabled, see branch work/2004
if false; then
for prog in dwgwrite dwgrewrite dxf2dwg dwg2dxf dxfwrite ; do
    for dwg in $DATA; do
        for as in --as=r13 --as=r2000 --as=r2004 --as=r2010 --as=r2013 --as=r2018; do
            run "$prog" "$dwg" "$as"
        done
    done
done
fi

case $PROGS in
  *dwggrep*)
    echo "@LTEXEC@ ./dwggrep -i -c tekst ${DATADIR}/example_*.dwg"
    if test "$(@LTEXEC@ ./dwggrep -i -c tekst "${DATADIR}"/example_*.dwg)" != "24"; then
      i=$((i+1))
      echo "dwggrep failure $?"
    fi

esac

#run dwgread r11/ACEL10

# TODO: check generated DXF's against original DXF's
# skip the run part if dwg2dxf is part of bin_PROGRAMS (i.e. being installed)
# rather see the dxf.test roundtrip test
# only in programs, not examples:
if [ -f cmp_dxf.pl ] && [ -e dwg2dxf ]; then
    for dwg in $DATA; do
        run dwg2dxf "$dwg"
        #if [ -f ${DATADIR}/$dwg.dxf ]; then
        #    base="$(basename "$dwg" .dwg)"
        #    perl filt_dxf.pl ${DATADIR}/$dwg.dxf > $base.dxf.orig
        #    perl filt_dxf.pl $base.dxf > $base.dxf.new
        #    diff -bu  $base.dxf.orig $base.dxf.new | perl cmp_dxf.pl
        #    rm $base.dxf.orig $base.dxf.new 2>/dev/null
        #fi
    done
fi

if test "0" = "$i" ; then
    exit 0
else
    echo "$(basename "$0"): $i failures"
    ls -l ./*.log
    exit 1
fi

# alive.test ends here
